﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Security.Cryptography;

namespace MD5Excel
{
    public partial class myForm : Form
    {
        public myForm()
        {
            InitializeComponent();
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            btStart.Enabled = false;
            btStart.Text = "生成中...";
            string fileName = "非结构化数据清单.xlsx";//保存的excel文件名
            bool isShowExcel = false;//是否显示Excel
            /*建立Excel对象*/
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            if (excel == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
                return;
            }
            try
            {
                excel.Application.Workbooks.Add(true);
                excel.Visible = isShowExcel;
                worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                worksheet.Cells[1, 1] = "fileid";
                worksheet.Cells[1, 2] = "filename";
                worksheet.Cells[1, 3] = "xxbsm";
                worksheet.Cells[1, 4] = "filepath_gx";
                worksheet.Cells[1, 5] = "filepath_jyt";
                worksheet.Cells[1, 6] = "filesize";
                worksheet.Cells[1, 7] = "createtime";
                worksheet.Cells[1, 8] = "filechecksum";
                worksheet.Cells[1, 9] = "fileintroduction";
                //遍历读取目录下的文件列表
                string current_path = System.Windows.Forms.Application.StartupPath;
                string[] files = Directory.GetFiles(current_path, "*.*", SearchOption.AllDirectories);
                //处理总数
                int total_amount = files.Length;
                // 当前行标记
                int rowIndex = 2;
                //循环写入数据
                foreach (string file in files)
                {
                    string file_name = System.IO.Path.GetFileName(file);
                    if (file_name != "Microsoft.Office.Interop.Excel.dll" && file_name != "MD5Excel.exe"&&file_name!=fileName)
                    {
                        //获取大小，md5值，文件名等信息
                        FileInfo fileInfo = new FileInfo(file);
                        string c=fileInfo.DirectoryName;
                        worksheet.Cells[rowIndex, 1]=System.Guid.NewGuid().ToString().Replace("-",""); //文件唯一标记
                        worksheet.Cells[rowIndex, 2] = file_name;
                        worksheet.Cells[rowIndex, 3] = "'4133012645";
                        worksheet.Cells[rowIndex, 4] = (fileInfo.DirectoryName+"\\").Replace(current_path, "");
                        //worksheet.Cells[rowIndex, 5] = file.Replace(current_path + "\\", "");
                        worksheet.Cells[rowIndex, 6] = fileInfo.Length;
                        worksheet.Cells[rowIndex, 7] = fileInfo.CreationTime;
                        worksheet.Cells[rowIndex, 8] = MD5Value(file);
                        rowIndex++;
                    }
                }
                worksheet.SaveAs(current_path +"\\"+fileName);//56为兼容模式，不加参数默认为当前xlsx格式。
                MessageBox.Show("成功导出Excel!");
                btStart.Enabled = true;
                btStart.Text = "开始生成";
               

            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (worksheet != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                excel.Quit();
                excel = null;
                GC.Collect();
            }
          
            return;
        }

        //获得文件MD5值，可以在别的程序访问该文件时使用。
        public string MD5Value(String filepath)
        {
             MD5 md5 = new MD5CryptoServiceProvider();
            byte[] md5ch;
            using (FileStream fs = File.OpenRead(filepath))
            {
                md5ch = md5.ComputeHash(fs);
            }
            md5.Clear();
            string strMd5 = "";
            for (int i = 0; i < md5ch.Length; i++)
            {
                strMd5 += md5ch[i].ToString("x").PadLeft(2, '0');
            }
            return strMd5;
        }
    }
}
